/**
 * 
 */
package fr.afcepf.ai90.Implementation;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import fr.afcepf.ai90.data.IDataPersonne;
import fr.afcepf.ai90.entity.Personne;
import fr.afcepf.ai90.util.SimpleDataSource;

/**
 * @author stagiaire
 * 
 */
public class DataPersonneImpl implements IDataPersonne {
    private final String resVerifConnection = "SELECT * FROM Personne p WHERE p.mail = ? AND p.mdp = ?";
    private final Integer indiceParamMail = 1;
    private final Integer indiceParamMdp = 2;
    private final String columnId = "id";
    private final String columnNom = "nom";
    private final String columnPrenom = "prenom";
    private final String columnDate = "date_naissance";
    private final String columnMail = "mail";
    private final String columnMdp = "mdp";

    /*
     * (non-Javadoc)
     * 
     * @see fr.afcepf.ai90.data.IDataPersonne#verifCommande(java.lang.String,
     * java.lang.String)
     */
    @Override
    public Personne verifCommande(String paramMail, String paramMdp) {
        DataSource data = new SimpleDataSource();
        Personne retour = null;

        try {
            Connection cnx = data.getConnection();
            PreparedStatement pst = cnx.prepareStatement(resVerifConnection);
            pst.setString(indiceParamMail, paramMail);
            pst.setString(indiceParamMdp, paramMdp);

            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                retour = new Personne();
                retour.setId(rs.getInt(columnId));
                retour.setNom(rs.getString(columnNom));
                retour.setPrenom(rs.getString(columnPrenom));
                retour.setDateNaisance(rs.getDate(columnDate));
                retour.setMail(rs.getString(columnMail));
                retour.setMdp(rs.getString(columnMdp));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return retour;
    }

}
